<?php namespace Phpcmf\Controllers\Admin;

class Home extends \Phpcmf\Common
{

    public function index() {

        $module = \Phpcmf\Service::L('cache')->get('module-'.SITE_ID.'-content');
        if (!$module) {
            $this->_admin_msg(0, dr_lang('未安装任何内容模块'));
        }

        $data = [];
        foreach ($module as $t) {
            $m = \Phpcmf\Service::L('cache')->get('module-'.SITE_ID.'-'.$t['dirname']);
            if ($m) {
                $is_field = 0;
                // 是否有字段
                $table = \Phpcmf\Service::M()->dbprefix(SITE_ID.'_'.$t['dirname']);
                if (\Phpcmf\Service::M()->is_field_exists($table, 'yuedusf') && \Phpcmf\Service::M()->is_field_exists($table, 'yuedusf_sku')) {
                    $is_field = 1;
                }
                $data[] = [
                    'name' => $t['name'],
                    'icon' => $t['icon'],
                    'dirname' => $t['dirname'],
                    'is_field' => $is_field,
                ];
            }
        }
        if (!$data) {
            $this->_admin_msg(0, dr_lang('未安装任何内容模块'));
        }

        \Phpcmf\Service::V()->assign([
            'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
                [
                    '内容模块权限' => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/index', 'fa fa-gears'],
                ]
            ),
            'data' => $data,
        ]);
        \Phpcmf\Service::V()->display('module_qx.html');
    }

    // 查看代码
    public function code_index() {

        $code1 = '
        
        {if \Phpcmf\Service::M(\'buy\', \'yuedusf\')->is_buy(MOD_DIR, $id, $member.id)}
        已经购买了：显示内容：{$content}
        {else}
        没有购买，阅读收费：{dr_payform("my-yuedusf_buy-".$id."-1-".MOD_DIR)}
        {/if}
        
        
        ';

        \Phpcmf\Service::V()->assign([
            'code1' => $code1,
        ]);
        \Phpcmf\Service::V()->display('code.html');

    }

    // 创建模块字段
    public function add() {

        $mid = dr_safe_filename($_GET['dir']);
        $row = \Phpcmf\Service::M('Module')->table('module')->where('dirname', $mid)->getRow();
        if (!$row) {
            $this->_json(0, '此模块[%s]未安装', $mid);
        }

        $module = \Phpcmf\Service::L('cache')->get('module-'.SITE_ID.'-'.$mid);
        if (!$module) {
            $this->_json(0, '此模块[%s]没有被安装', $mid);
        }

        // 创建字段

        $table = \Phpcmf\Service::M()->prefix.SITE_ID.'_'.$mid;
        if (!\Phpcmf\Service::M()->db->fieldExists('yuedusf', $table)) {
            \Phpcmf\Service::M()->query('ALTER TABLE `'.$table.'` ADD `yuedusf` varchar(100) DEFAULT 0 COMMENT \'阅读付费字段\'');
        }
        if (!\Phpcmf\Service::M()->db->fieldExists('yuedusf_sku', $table)) {
            \Phpcmf\Service::M()->query('ALTER TABLE `'.$table.'` ADD `yuedusf_sku` TEXT DEFAULT NULL COMMENT \'阅读付费字段\'');
        }
        if (!\Phpcmf\Service::M()->db->table('field')->where('fieldname', 'yuedusf')->where('relatedid', $row['id'])->where('relatedname', 'module')->countAllResults()) {
            \Phpcmf\Service::M()->db->table('field')->insert(array(
                'name' => '阅读付费',
                'ismain' => 1,
                'setting' => '',
                'issystem' => 1,
                'ismember' => 1,
                'disabled' => 0,
                'fieldname' => 'yuedusf',
                'fieldtype' => 'score',
                'relatedid' => $row['id'],
                'relatedname' => 'module',
                'displayorder' => 0,
            ));
        }

        // 创建表
        // 判断是否存在表
        $ctable = $table.'_yuedusf';
        \Phpcmf\Service::M()->db->simpleQuery(trim("DROP TABLE IF EXISTS `".$ctable."`"));
        \Phpcmf\Service::M()->db->simpleQuery(trim("CREATE TABLE IF NOT EXISTS `".$ctable."` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `cid` int(10) unsigned NOT NULL COMMENT '文档id',
      `buy_uid` mediumint(8) unsigned NOT NULL COMMENT '支付uid',
      `sell_uid` mediumint(8) unsigned NOT NULL COMMENT '作者uid',
      `value` decimal(10,2) unsigned NOT NULL COMMENT '付款金额',
      `inputtime` int(10) unsigned NOT NULL COMMENT '录入时间',
      PRIMARY KEY (`id`),
      KEY `cid` (`cid`),
      KEY `buy_uid` (`buy_uid`),
      KEY `sell_uid` (`sell_uid`),
      KEY `inputtime` (`inputtime`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模块阅读收费插件操作记录表';"));

        \Phpcmf\Service::M('cache')->sync_cache('');
        $this->_json(1, '安装成功');
    }

    // 删除模块字段
    public function del() {

        $mid = dr_safe_filename($_GET['dir']);
        $row = \Phpcmf\Service::M('Module')->table('module')->where('dirname', $mid)->getRow();
        if (!$row) {
            $this->_json(0, '此模块[%s]未安装', $mid);
        }

        $module = \Phpcmf\Service::L('cache')->get('module-'.SITE_ID.'-'.$mid);
        if (!$module['field']['yuedusf']) {
            $this->_json(0, '此模块[%s]没有安装', $mid);
        }

        $table = \Phpcmf\Service::M()->prefix.SITE_ID.'_'.$mid;
        if (\Phpcmf\Service::M()->db->fieldExists('yuedusf', $table)) {
            \Phpcmf\Service::M()->query('ALTER TABLE `'.$table.'` DROP `yuedusf`;');
        }
        if (\Phpcmf\Service::M()->db->fieldExists('yuedusf_sku', $table)) {
            \Phpcmf\Service::M()->query('ALTER TABLE `'.$table.'` DROP `yuedusf_sku`;');
        }

        // 判断是否存在表
        $ctable = $table.'_yuedusf';
        if (\Phpcmf\Service::M()->db->tableExists($ctable)) {
            \Phpcmf\Service::M()->db->simpleQuery(trim("DROP TABLE IF EXISTS `".$ctable."`"));
        }

        \Phpcmf\Service::M()->db->table('field')->where('fieldname', 'yuedusf')->where('relatedid', $row['id'])->where('relatedname', 'module')->delete();

        \Phpcmf\Service::M('cache')->sync_cache('');
        $this->_json(1, '卸载成功');
    }
}
